package com.aaa.stock.controller.api;


import com.aaa.entity.Producer;
import com.aaa.stock.service.ProducerService;
import com.aaa.stock.vo.Producervo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("stock/api/user")
public class ApiProducerController {
   @Autowired
   private ProducerService producerService;
   @PostMapping("searchUser/{currentPage}/{pageSize}")
   public Result<IPage<Producer>> searchUser(
           @PathVariable Integer currentPage,
           @PathVariable Integer pageSize,
           @RequestBody Producervo producervo
   ){
      System.out.println("==============================================="+producervo);
      return  producerService.findByCondition(currentPage,pageSize,producervo);
   }

   @PostMapping("addstu")
   public Result addstu(@RequestBody Producer producer) {
      if (Objects.nonNull(producer.getProducerId())){
         producer.setUpdateTime(LocalDateTime.now());
      }else {
         producer.setCreateTime(LocalDateTime.now());
      }
       producerService.saveOrUpdate(producer);
      return new Result<>(200,"成功");
   }


   @DeleteMapping("/{producerId}")
   public Result delStu(@PathVariable Integer producerId){
      producerService.removeById(producerId);
     return new Result<>(200,"删除成功");
   }



   //查询
   @GetMapping("getAll")
   public Result<List<Producer>> getAll(){
      return  producerService.getAll();
   }


   @PostMapping("/del1")
   public Result delStu1(@RequestBody List<Producer> producers){
      producers.forEach((Producer ppp)->{
         boolean b = producerService.removeById(ppp.getProducerId());
      });
      return new Result(200,"成功");
   }
}
